1157번 단어 공부
Day6 6단계 20231024
- 이번엔 Stream, Collections를 사용했다
- 속도가 868ms 매우 느리다.. => 스트림을 고집할 필요가 없을 듯 하다.
- (오히려 배열에서 요소를 하나씩 불러와서 보는게 더 빠르다.. 260ms까지 나옴)
- 스트림API 항목에 왜 그런지 참고 자료와 내용을 정리했다.
import java.io.*;
import java.util.*;
import java.util.Map.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().toUpperCase().split("");
List<String> listStr = Stream.of(str).collect(Collectors.toList());
Map<String, Integer> mapStr = Stream.of(str).distinct()
.collect(Collectors.toMap(i -> i,
i -> Collections.frequency(listStr, i.toString())));
String frqStr = "";
int frq = 0;
for (Entry<String, Integer> entry : mapStr.entrySet()) {
if (entry.getValue() > frq) {
frqStr = entry.getKey();
frq = entry.getValue();
} else if (entry.getValue() == frq) {
frqStr = "?";
}
}
System.out.println(frqStr);
br.close();
}
}